import 'package:dio/dio.dart';
import 'package:ncov_2019/commom/user.dart';
import 'package:ncov_2019/config/api.dart';
import 'package:ncov_2019/http/dio_manager.dart';
import 'package:ncov_2019/http/UserModel.dart';


class ApiService {

  /// 登录
  void login(Function callback, String _username, String _password) async {
    FormData formData =
        new FormData.from({"username": _username, "password": _password});
    DioManager.singleton.dio
        .post(API.USER_LOGIN, data: {"username": _username, "password": _password}, options: _getOptions())
        .then((response) {
      callback(UserModel.fromJson(response.data), response);
    });
  }

  /// 注册
  void register(Function callback, String _username, String _password) async {
    FormData formData = new FormData.from({
      "username": _username,
      "password": _password,
      "uuid": _password
    });
    DioManager.singleton.dio
        .post(API.USER_REGISTER, data: {"username": _username, "password": _password}, options: null)
        .then((response) {
      print(response.toString());
      callback(UserModel.fromJson(response.data));
    });
  }

  void getUUID(Function callback) async {
    DioManager.singleton.dio
        .get(API.GET_UUID)
        .then((response) {
      print(response.toString());
      callback(response.message);
    });
  }


  Options _getOptions() {
    Map<String, String> map = new Map();
    List<String> cookies = User().cookie;
    map["Cookie"] = cookies.toString();
    return Options(headers: map);
  }
}
